﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WindowsFormsApp2
{
    class DbHelper
    {
        public static String user = "server =.;database=Student ;uid = sa ; pwd=123456";
        /// <summary>
        /// DataReader用法
        /// </summary>
        /// <param name="cmdString"></param>
        /// <returns></returns>
        public static SqlDataReader Query(string cmdString)
        {
            SqlConnection sqlConnection = new SqlConnection(user);

            using (SqlCommand dql = new SqlCommand(cmdString, sqlConnection))
            {
                sqlConnection.Open();
                return dql.ExecuteReader(CommandBehavior.CloseConnection);
            }
        }

        //ExecuteNonQuery();
        /// <summary>
        /// 增加或者修改方法
        /// </summary>
        /// <param name="cmdString"></param>
        /// <returns></returns>
        public static int AddOrEditSave(string cmdString)
        {
            SqlConnection sqlConnection = new SqlConnection(user);

            using (SqlCommand sql = new SqlCommand(cmdString, sqlConnection))
            {
                sqlConnection.Open();
                return sql.ExecuteNonQuery();
            }
        }

        //DataTable();
        /// <summary>
        /// 获取表数据
        /// </summary>
        /// <param name="cmdString"></param>
        /// <returns></returns>
        public static DataTable GetDataTable(string cmdString)
        {
            SqlConnection sql = new SqlConnection(user);

            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdString, sql);

            DataTable dt = new DataTable();

            sql.Open();

            sqlDataAdapter.Fill(dt);

            return dt;
        }
    }
}
